/**
 * Copyright 2008 Matthew DeLambo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.delambo.drool.pooling;

import com.delambo.drool.interfaces.*;
import com.delambo.drool.modules.*;
import com.delambo.drool.logging.*;
import com.delambo.drool.exceptions.*;
import com.google.inject.*;
import java.util.*;

/**
 * Provides a drool pool implementation. This is not the classic Gang of Four 
 * factory pattern; this is a cheap and quick way to give the client a static
 * handle to create a new drool implementation without forcing the client to 
 * work with/depend on Guice (Although, they should :).
 * @author delambo
 */
public class DroolFactory {
    
    /**
     * Enforce the static nature of this utility factory.
     */
    private DroolFactory() {
        throw new AssertionError();
    }
    
    /**
     * Returns a new Drool implementation constructed with one pool using the 
     * given poolPropsFile.
     * @param logPropsFile logging properties file for new pool.
     * @param poolPropsFile pool properties file for new pool.
     * @param id identifier for pool.
     * @return Drool_Impl constructed with one pool.
     */
    public static <ID> Drool<ID, Pool_Impl> create(String logPropsFile, 
            String poolPropsFile, ID id) throws DroolException {
        Injector injector = Guice.createInjector(new Drool_Module(),
                new ImmutableMap_Module(), new PropertyMap_Module(), 
                new Logger_Module());
        Drool.Factory df = injector.getInstance(Drool.Factory.class);
        return df.create(logPropsFile, poolPropsFile, id);
    }
    
    /**
     * Returns a new drool impl constructed with pools mapped in the given map.
     * @param logPropsFile path for logging properties file.
     * @param p map of of multiple [identifiers, propertyfile paths]
     * @return Drool_Impl constructed with multiple pools.
     */
    public static <ID> Drool<ID, Pool_Impl> create(String logPropsFile, 
            HashMap p) throws DroolException {
        Injector injector = Guice.createInjector(new Drool_Module(),
                new ImmutableMap_Module(), new PropertyMap_Module(), 
                new Logger_Module());
        Drool.Factory df = injector.getInstance(Drool.Factory.class);
        return df.create(logPropsFile, p);
    }
}
